package com.bwei.mypaot.menu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bwei.mypaot.R;
import com.bwei.mypaot.base.activity.BaseActivity;
import com.bwei.mypaot.base.activity.MyApplication;
import com.bwei.mypaot.base.bean.SignBean;
import com.bwei.mypaot.utils.Constant;
import com.bwei.mypaot.utils.DESMD5Utils;
import com.bwei.mypaot.utils.OKHttpSingleTop;
import com.bwei.mypaot.view.MyEditText;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.bwei.mypaot.base.activity.MyApplication.oKHttpSingleTop;

/**
 * Created by Administrator on 2017/2/17.
 */

public class RegisterActivity extends BaseActivity {

    private MyEditText et_phone;
    private MyEditText et_message;
    private MyEditText et_pass;
    private MyEditText et_repass;
    private Button bt_yzm;
    private Button bt_tj;
    private EditText et_name;
    private EditText et_user_district;
    private String decryptname;
    private String decryptphone;
    private String md5pass;
    private String despass;
    private String desdistrict;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        initHeader();
        initWidget();
        setWidgetState();
    }

    @Override
    public void initHeader() {
        inittHeaderWidget();
        addImageLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    public void initWidget() {
        et_phone = (MyEditText) findViewById(R.id.et_phone);
        et_message = (MyEditText) findViewById(R.id.et_message);
        bt_yzm = (Button) findViewById(R.id.bt_yzm);
        bt_tj = (Button) findViewById(R.id.bt_tj);
        et_pass= (MyEditText)findViewById(R.id.et_pass);
        et_repass=(MyEditText)findViewById(R.id.et_repass);
        et_name = (EditText) findViewById(R.id.et_name);
        et_user_district = (EditText) findViewById(R.id.et_user_district);

    }

    @Override
    public void setWidgetState() {
        et_phone.setOnClickListener(this);
        et_message.setOnClickListener(this);
        bt_yzm.setOnClickListener(this);
        bt_tj.setOnClickListener(this);
        et_pass.setOnClickListener(this);
        et_repass.setOnClickListener(this);
        et_name.setOnClickListener(this);
        et_user_district.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()){
            case R.id.bt_yzm:




                break;
            case R.id.bt_tj:
                String phone=et_phone.getText().toString().trim();
                String name=et_name.getText().toString().trim();
                String district=et_user_district.getText().toString().trim();
                String  pass= et_pass.getText().toString().trim();
                String  repass= et_repass.getText().toString().trim();

                DESMD5Utils des=new DESMD5Utils();
                //昵称和电话号码des加密
                decryptname = des.encrypt(name);
                Log.d("a",decryptname);
                decryptphone = des.encrypt(phone);
                //密码md5和des双重加密
                md5pass = DESMD5Utils.MD5(pass, true);
                despass = des.encrypt(md5pass);

                //区域des加密
                desdistrict = des.encrypt(district);
                //把解密后的数据存入集合传给请求方法

                if (map!=null){
                    map.clear();
                }else{
                    map.put("nick_name",decryptname);
                    map.put("phone_num",decryptphone);
                    map.put("user_password",despass);
                    map.put("user_district",desdistrict);
                }

//                map.put("phone_num",repass);
                String url= Constant.WJF_SERVICE+Constant.ServiceConstant.Receive_REGISTER;
                oKHttpSingleTop.setUrl(url,map, SignBean.class,OKHttpSingleTop.Methods.POST);
                oKHttpSingleTop.setCallbackM(new OKHttpSingleTop.CallbackM() {
                    @Override
                    public void onFailure(Request request, IOException e) {

                    }

                    @Override
                    public void onResponse(Object response) {
                        Toast.makeText(RegisterActivity.this,(String) response,Toast.LENGTH_SHORT).show();
                        Log.d("KKK", (String) response);
                        SignBean sinBean= (SignBean)response;
                        if (sinBean.id_card.equals(007000)){
                            Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }


                    }
                });

                break;



        }




    }
}
